
#ifndef MYXMPPCLIENT_H
#define MYXMPPCLIENT_H

#include "QXmppClient.h"
#include "QXmppVCardIq.h"
#include "QXmppVCardManager.h"
#include "QXmppMessageReceiptManager.h"

#include <QObject>
#include <QtDeclarative>
#include <QDeclarativeView>
#include <QMap>
#include <QList>
#include <QVariant>

#include "rosteritemmodel.h"
#include "rosterlistmodel.h"

#include "chatsitemmodel.h"
#include "chatslistmodel.h"

#include "mycache.h"
#include "messagewrapper.h"
#include "meegimsettings.h"
#include "qmlxmppclient.h"

/****************/
/* http://www.developer.nokia.com/Community/Wiki/Workaround_to_hide_VKB_in_QML_apps_%28Known_Issue%29 */
class EventFilter : public QObject
{
protected:
    bool eventFilter(QObject *obj, QEvent *event) {
        QInputContext *ic = qApp->inputContext();
        if (ic)
        {
            if ( (ic->focusWidget() == 0) && prevFocusWidget) {
                QEvent closeSIPEvent( QEvent::CloseSoftwareInputPanel );
                ic->filterEvent(&closeSIPEvent);
            } else if ( (prevFocusWidget == 0) && (ic->focusWidget()) ) {
                QEvent openSIPEvent( QEvent::RequestSoftwareInputPanel );
                ic->filterEvent(&openSIPEvent);
            }
            prevFocusWidget = ic->focusWidget();
        }
        return QObject::eventFilter(obj,event);
    }

private:
    QWidget *prevFocusWidget;
};
/****************/

typedef QMap<QString, QVariant> Map;

class MyXmppClient : public QDeclarativeView
{
    Q_OBJECT

    MyCache *cacheIM;
    MessageWrapper *msgWrapper;

    ChatsListModel *listModelChats;

    QXmppClient *xmppClient;
    QXmppRosterManager *rosterManager;
    QXmppVCardManager *vCardManager;
    QXmppMessageReceiptManager * xmppMessageReceiptManager;

    QString xmppUsername;
    QString xmppServername;
    QString xmppJid;
    QString xmppPassword;

    QMap< QString, RosterItemModel*> listRoster;
    RosterListModel *listModelRoster;

    bool flShowOffline;
    void setQMLListRoster();

    MeegIMSettings *mimOpt;

    QString cppMeegIMStatus;

    QMLXmppClient *qmlXmppClient;

public :
    explicit MyXmppClient();
    ~MyXmppClient();

    void initXmppClient();

    inline void setJid( const QString &jid ) { xmppJid = jid; }
    inline void setPasswd( const QString &passwd ) { xmppPassword = passwd; }

    void connectToXmppServer();

    Q_INVOKABLE void setMyPresence( QString status, QString textStatus );
    Q_INVOKABLE void typingStart( QString bareJid );
    Q_INVOKABLE void typingStop( QString bareJid );

    Q_INVOKABLE QString __getStatus() const { return cppMeegIMStatus; }
    Q_INVOKABLE QString __getStatusText() const { return cppMeegIMStatus; }

    static QString myJid;
    static QString myPass;
    static QString myVersion;

signals:

public slots:
    void clientConnected();
    void clientStateChanged(QXmppClient::State state);

    Q_INVOKABLE void openChat( QString jid );
    Q_INVOKABLE void closeChat( QString jid );

private slots:
    void initRoster();
    void initPresence(const QString& bareJid, const QString& resource);
    void initVCard(const QXmppVCardIq &vCard);

private:
    QString getPicPresence( const QXmppPresence::Status::Type &presence ) const;
    QString getTextStatus(const QString &textStatus, const QXmppPresence::Status::Type &status ) const;
};

#endif
